package com.tinyideatech.admin.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tinyideatech.common.core.controller.BaseController;
import com.tinyideatech.common.core.entity.ApiResult;
import com.tinyideatech.common.core.entity.PageResult;
import com.tinyideatech.common.core.text.Convert;
import com.tinyideatech.framework.utils.ShiroUtils;
import com.tinyideatech.system.entity.SysRole;
import com.tinyideatech.system.entity.SysUser;
import com.tinyideatech.system.entity.SysUserRole;
import com.tinyideatech.system.service.ISysRoleService;
import com.tinyideatech.system.service.ISysUserRoleService;
import com.tinyideatech.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/system/user")
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysUserRoleService userRoleService;

    @Autowired
    private ISysRoleService roleService;

    @GetMapping()
    public String userList() {
        return "system/user/list";
    }

    @GetMapping("/add")
    public String userAdd(
            ModelMap map
    ) {
        List<SysRole> roleList = roleService.list();
        map.addAttribute("roleList", roleList);
        return "system/user/add";
    }

    @GetMapping("/edit/{id}")
    public String userEdit(
            @PathVariable("id") Long id,
            ModelMap map
    ) {
        SysUser user = userService.getById(id);
        List<SysRole> roleList = roleService.list();
        user.setRoleIds(userService.getRoleIds(user));
        map.addAttribute("user", user);
        map.addAttribute("roleList", roleList);
        return "system/user/edit";
    }

    @GetMapping("/resetPwd/{id}")
    public String resetPwd(
            @PathVariable("id") Long id,
            ModelMap map
    ) {
        SysUser user = userService.getById(id);
        map.addAttribute("user", user);
        return "system/user/resetPwd";
    }

    @ResponseBody
    @PostMapping("/resetPwd")
    public ApiResult resetPwd(
            SysUser sysUser
    ) {
        SysUser user = userService.getById(sysUser.getUserId());
        user.setPassword(ShiroUtils.getPassword(sysUser.getPassword()));
        userService.updateById(user);
        return success();
    }

    @ResponseBody
    @PostMapping("/list")
    public PageResult list(SysUser user) {
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
//        if(StringUtils.isNotEmpty(user.getUserName())){
//            qw.like("user_name", user.getUserName());
//        }
        IPage<SysUser> page = startPage();
        IPage<SysUser> userList = userService.page(page, qw);
        return page(userList);
    }

    @ResponseBody
    @PostMapping("/save")
    public ApiResult save(
            SysUser sysUser
    ) {
        userService.saveOrUpdate(sysUser);
        userRoleService.removeUserRole(sysUser);
        if(sysUser.getRoleIds() != null && !sysUser.getRoleIds().equals("")){
            List<SysUserRole> urList = userService.setUserRoleList(sysUser);
            if(urList.size() > 0){
                userRoleService.saveOrUpdateBatch(urList);
            }
        }
        return success();
    }

    @ResponseBody
    @PostMapping("/remove")
    public ApiResult remove(
            String ids
    ){
        Long[] userIds = Convert.toLongArray(ids);
        userService.removeByIds(Arrays.asList(userIds));
        return success();
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public ApiResult changeStatus(SysUser user)
    {
        userService.changeStatus(user);
        return success();
    }

}
